package com.seeTools.next;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.seeTools.next.api.entity.TenantBaseEntity;

import java.sql.SQLException;
import java.util.Collections;

public class CodeGenerator {

    private static final String URL = "jdbc:mysql://localhost:3306/charging-dev?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";

    private static final String USERNAME = "root";
    private static final String PASSWORD = "zfy123456";
    private static final String BASE_PACKAGE = "com.seeTools.next";
    private static final String SRC_JAVA_PATH = "./next/src/main/java";
    private static final String MAPPER_XML_PATH = "./next/src/main/resources/mapper";

    private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder(URL, USERNAME, PASSWORD)
            .schema("charging-dev");

    public static void main(String[] args) throws SQLException {
        // 初始化数据库脚本
        FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                // 全局配置
                .globalConfig((scanner, builder) ->
                        builder.author(scanner.apply("请输入作者名称"))
                                .outputDir(SRC_JAVA_PATH)
                                .disableOpenDir()
                )
                // 包配置
                .packageConfig((scanner, builder) ->
                        builder.parent(BASE_PACKAGE)
                                .pathInfo(Collections.singletonMap(OutputFile.xml, MAPPER_XML_PATH))
                )
                // 策略配置
                .strategyConfig((scanner, builder) -> builder.addInclude(scanner.apply("请输入表名，多个表名用,隔开"))
                        .entityBuilder()
                        .logicDeleteColumnName("deleted")
                        .superClass(TenantBaseEntity.class)
                        .enableLombok()
                        .enableChainModel()
                        .addSuperEntityColumns("id", "tenant_id", "create_by", "create_time", "update_by", "update_time", "deleted")
                        .controllerBuilder()
                        .enableRestStyle()
                )
                .execute();
    }
}
